package com.aidong.videoComment.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.aidong.util.PageUtil;
import com.aidong.videoComment.biz.TrnVideoCommentBiz;
import com.aidong.videoComment.entity.TrnVideoComment;
import com.aidong.videoComment.entity.VideoComment;
import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("json")
@Namespace("/")
public class TrnVideoCommentAction implements ModelDriven<TrnVideoComment> {
	private TrnVideoComment trnVideoComment;
	
	@Override
	public TrnVideoComment getModel() {
		if (trnVideoComment == null) {
			trnVideoComment = new TrnVideoComment();
		}
		return trnVideoComment;
	}
	private Map<String, Object> map;

	@JSON
	public Map<String, Object> getMap() {
		return map;
	}
	private TrnVideoCommentBiz trnVideoCommentBiz; 
	@Autowired
	public void setTrnVideoCommentBiz(TrnVideoCommentBiz trnVideoCommentBiz) {
		this.trnVideoCommentBiz = trnVideoCommentBiz;
	} 
	
	@Action(value = "addVideoComment", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String addVideoComment() {
		map = new HashMap<String, Object>();
		String msg="";
		if(trnVideoComment.getVideoId()==0){
			msg="视频编码为空！";
		}else if(trnVideoComment.getIdongId()==0){
			msg="评论人爱动号为空！";
		}else if(StringUtils.isBlank(trnVideoComment.getComment())){
			msg="评论内容为空！";
		}
		
		if(StringUtils.isBlank(msg)){
			try{
				trnVideoCommentBiz.addVideoComment(trnVideoComment);
				map.put("code", 1);
				map.put("result", "评论成功");
				return "succ";
			} catch(Exception e) {
				map.put("code", 0);
				map.put("msg", "系统异常");
				return "succ";
			}
		}else{
			map.put("code", 0);
			map.put("msg", "参数缺失");
			return "succ";
		}
	}
	@Action(value = "getVideoCommentlList", results = { @Result(name = "succ", type = "json", params = { "root", "map" }) })
	public String getVideoCommentlList() {
		map = new HashMap<String, Object>();
		
		if(trnVideoComment.getVideoId() > 0){
			try{
				Map<String, Object> pageMap = PageUtil.page(10, trnVideoComment.getPageCurrent());
				pageMap.put("videoId",trnVideoComment.getVideoId());
				List<VideoComment> list=trnVideoCommentBiz.getVideoCommentlList(pageMap);
				for(VideoComment vc : list){
					vc.setTime(getDay(vc.getComTime()));
				}
				map.put("code", 1);
				map.put("result", list);
				return "succ";
			} catch(Exception e) {
				map.put("code", 0);
				map.put("msg", "系统异常");
				return "succ";
			}
		}else{
			map.put("code", 0);
			map.put("msg", "参数缺失");
			return "succ";
		}
	}
	
public static String getDay(Date begin){
		
		long nd = 1000 * 24 * 60 * 60;
	    long nh = 1000 * 60 * 60;
	    long nm = 1000 * 60;
	    // long ns = 1000;
	    // 获得两个时间的毫秒时间差异
	    long diff =  new Date().getTime()-begin.getTime();
	    // 计算差多少天
	    long day = diff / nd;
	    if(day>0 && day<=7){
	    	return day+"天前";
	    }else if(day>7){
	    	SimpleDateFormat sm=new SimpleDateFormat("yyyy-MM-dd");
	    	return sm.format(begin);
	    }
	    
	    // 计算差多少小时
	    long hour = diff % nd / nh;
	    if(hour<1){
	    	return "刚刚";
	    }else/* if(hour>=1 && hour<=23)*/{
	    	return hour+"小时前";
	    }
	}
}
